package com.mrd.gtimpframe.service.impl;

import com.mrd.base.Jqgrid;
import com.mrd.gtimpframe.entity.CustomCompanyInfo;
import com.mrd.gtimpframe.repository.CusInfoMapper;
import com.mrd.gtimpframe.service.CusInfoService;
import com.mrd.util.FormBeanUtil;
import com.mrd.util.LoginUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.data.domain.Page;


/**
 * Created by dusy on 2017/7/27.
 * @author kun
 */
@Service
public class CusInfoServiceImpl implements CusInfoService {
    @Resource
    private CusInfoMapper cusInfoMapper;

    @Override
    public int deleteByPrimaryKey(String comInfoId) {
        return cusInfoMapper.deleteByPrimaryKey(comInfoId);
    }


    @Override
    public Page<CustomCompanyInfo> selectSelective(Jqgrid jqgrid) {
        jqgrid.pageWithMybatis();
        jqgrid.sortWithMybatis();
        String comInfoId = LoginUtil.getLoginUser().getCompany().getId();
        CustomCompanyInfo params = FormBeanUtil.getEntityFromJqgrid(jqgrid, CustomCompanyInfo.class);
        if (comInfoId!=null){
            params.setComInfoId(comInfoId);
        }
        if (!"".equals(params.getCtimeEnd())&&params.getCtimeEnd()!=null){
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            try {
                params.setCtimeEnd(sdf.format(sdf.parse(params.getCtimeEnd()).getTime()+60*60*24*1000));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        List<CustomCompanyInfo> data =cusInfoMapper.selectSelective(params);
        /*List<CustomCompanyInfo> newdata=new ArrayList<>();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i=0;i<data.size();i++){
             CustomCompanyInfo cus=new CustomCompanyInfo();
             CustomCompanyInfo customCompanyInfo=data.get(i);
             cus.setCreateTime(sdf.format(customCompanyInfo.getCtime()));
             cus.setComInfoId(customCompanyInfo.getComInfoId());
             cus.setUniteCode(customCompanyInfo.getUniteCode());
             cus.setCustomsCode(customCompanyInfo.getCustomsCode());
             cus.setCreator(customCompanyInfo.getCreator());
             cus.setAgentRefId(customCompanyInfo.getAgentRefId());
             cus.setName(customCompanyInfo.getName());
             cus.setComValidate(customCompanyInfo.getComValidate());
             newdata.add(cus);
        }*/
        return jqgrid.pageResult(data);
    }

    @Override
    public List<CustomCompanyInfo> selectSelectiveCompId(CustomCompanyInfo customCompanyInfo) {
        return cusInfoMapper.selectSelectiveCompId(customCompanyInfo);
    }

    @Override
    public String selectAgentNameById(String comId) {
        CustomCompanyInfo customCompanyInfo=cusInfoMapper.selectComNameById(comId);
        String agentName=customCompanyInfo.getName();
        return agentName;
    }

    @Override
    public String selectComTypeById(String comId) {
        CustomCompanyInfo customCompanyInfo=cusInfoMapper.selectCustType(comId);
        String comType=customCompanyInfo.getIsDrectCust();
        return comType;
    }
}
